<?php
/**
 * @ignore
 */
class CliController extends CI_Controller {
    protected function _auth()
    {
		if (!is_cli()) show_404();
		//处理日志权限问题
		umask(0);
	}
	
	function genAuth()
	{
		$path=trim($_SERVER['argv'][2],'/');
		$path = explode('/','/'.$path);
		$file = ucfirst(array_pop($path));
		$apiDir = implode("/", $path) . "/";
		require_once(__DIR__ . $apiDir . $file . ".php");
		$className = $file.'Controller';
		$methodName = get_class_methods($className);
		$methodName = array_diff($methodName,['get_instance','getRules']);
		$insert=[];
		$pre=strtolower(substr($apiDir . $file,1)).'/';
		foreach ($methodName as $mValue){
			$rMethod = new Reflectionmethod($className, $mValue);
			if (!$rMethod->isPublic() || strpos($mValue, '_') === 0) {
				continue;
			}
			$rights=['name'=>'','index'=>$pre.strtolower($mValue)];
			$docComment = $rMethod->getDocComment();
			if ($docComment !== false) {
				$docCommentArr = explode("\n", $docComment);
				$comment = trim($docCommentArr[1]);
				$title = trim(substr($comment, strpos($comment, '*') + 1));
				$rights['name']=$title;
			}
			$insert[]=$rights;
		}
		$this->db->insert_batch('rights',$insert,true,true);
	}
}
